<template>
  <div class="app-container">
    <el-form @submit.native.prevent :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
      label-width="68px" >
    <!-- 书籍搜索框 -->
      <el-form-item label="书籍名称" prop="name">
        <el-input v-model="queryParams.searchByName" placeholder="请输入书籍名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
    <!--刷新按钮 -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">刷新</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['system:post:add']">新增</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['system:post:edit']">修改</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleMoreDelete"
          v-hasPermi="['system:post:remove']">删除</el-button>
      </el-col>

    </el-row>

    <el-table v-loading="loading" size="medium " :data="postList" @selection-change="handleSelectionChange" stripe >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="书籍ID"  width="100" align="center" prop="bookId" />
      <el-table-column label="书籍名称" width="180" align="center" prop="name" />
      <el-table-column label="作者" width="150" align="center" prop="author" />
      <el-table-column label="语言" width="100" align="center" prop="language" />
      <el-table-column label="出版社" width="150" align="center" prop="publish" />
      <el-table-column label="库存" width="100" align="center" prop="number">
      </el-table-column>
      <el-table-column label="创建时间"  align="center" prop="createTime" width="150">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" fixed="right"   min-width="200" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini " type="primary" plain icon="el-icon-info" @click="handleInfo(scope.row)"
           >详情</el-button>
          <el-button size="mini " type="warning" plain icon="el-icon-edit" @click="handleUpdate(scope.row)"
            >修改</el-button>
          <el-button size="mini" type="danger"    icon="el-icon-delete" @click="handleDelete(scope.row)"
           >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
    <!-- 点击详情弹出框 -->
    <el-dialog title="图书详情" width="80%"   :visible.sync="isShowInfo" center  custom-class="infoDialog">
      <infoTable v-show="isShowInfo" :infoData="infoData" />
      <span slot="footer" class="dialog-footer">
        <el-button @click="isShowInfo = false">取 消</el-button>
        <el-button type="primary" @click="isShowInfo = false">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 添加书籍对话框 -->
    <addBookTable  v-if="isShowAdd" @closeAddTable="handleCloseAddTable"/>
    <!-- 修改书籍对话框 -->
    <editTable v-if="isShowEdit" @closeEditTable="handleCloseEditTable"  :editData="editData"/>
  </div>
</template>

<script>

import { listPost, getPost, delPost, addPost, updatePost } from "@/api/system/post";
import Pagination from '@/components/Pagination/index'
import { getAllBook, addPublishAndLanguageDict ,delBook,getDict,addDict} from '@/api/request.js'
import editTable from '@/components/ManagementTable/editTable.vue'
import infoTable from '@/components/ManagementTable/infoTable.vue'
import addBookTable from '@/components/ManagementTable/addBookTable.vue'
import imageUpload from '@/components/ManagementTable/ImageUpload.vue'
export default {
  name: "bookManagement",
  dicts: ['sys_normal_disable'],
  components: {
    infoTable,
    imageUpload,
    addBookTable,
    editTable
  },
  data() {
    return {
      //test
      testquery:{
        type:'publish'
      },
      //需要修改的图书信息
      editData:{},
      // 是否显示修改图书弹框
      isShowEdit:false,
      //是否显示添加图书弹框
      isShowAdd:false,
      //详情书籍的信息
      infoData: {},
      //详情弹出框
      isShowInfo: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 书籍表格数据
      postList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询书籍参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        searchByName: "",

      },
      // 表单参数
      form: {},
     
    };
  },
  created() {
    this.getList();
  },
  methods: {




    //根据bookId删除图书
    delBookById(){
      this.loading = true;
      delBook({"bookIds":this.ids}).then(res=>{
        this.loading = false;
        console.log("删除接口调用成功,删除的书籍编号",this.ids)
        //删除完书籍后选中ids清空
        this.ids=[]
        this.getList()
      })
    },
    //关闭显示添加图书弹框,并刷新数据
    handleCloseAddTable(){
      this.isShowAdd = false
      // 并刷新数据
      this.getList()
    },
     //关闭显示修改图书弹框,并刷新数据
     handleCloseEditTable(){
      this.isShowEdit = false
      // 并刷新数据
      this.getList()
    },
    // // 重置infoData
    restInfoData() {
      this.infoData = {}
    },
    /** 查询书籍列表 */
    getList() {
      this.loading = true;
      getAllBook(this.queryParams).then(res => {
        this.postList = res.rows
        // this.total = 59
        this.total = res.total
        this.loading = false;
        // console.log('查询书籍接口调用成功',res)
      }).catch(err => {
        console.log("查询书籍接口调用失败")
      })
    },
    // 取消按钮
    cancel() {
      this.addTableVisible = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        postId: undefined,
        postCode: undefined,
        postName: undefined,
        postSort: 0,
        status: "0",
        remark: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      // 将刷新时携带的搜索关键字清空，防止查询关键字
      this.queryParams.searchByName = ""
      this.getList()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.bookId)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.isShowAdd = true
      this.reset();
    },
    /** 详情按钮操作 */
    handleInfo(row) {
      this.restInfoData()
      this.isShowInfo = true
      this.infoData = row
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.editData = row
      this.isShowEdit = true
      console.log('editData',this.editData)
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm('此操作将永久删除编号为'+row.bookId+ '的书籍，是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(()=>{
          this.ids.push(row.bookId)
          this.delBookById()
          this.$message({
            type:'success',
            message:'删除成功！'
          })
        })


      // this.ids.push(row.bookId)
      // this.delBookById()
    },
    /**
     *@functionName:     handleMoreDelete
     *@description:批量删除书籍处理
     *@author: zxh
     *@date: 2024-01-08 14:17:12
    */
    handleMoreDelete(){
      this.$confirm('此操作将永久删除编号为'+this.ids+ '的书籍，是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(()=>{
          this.delBookById()
          this.$message({
            type:'success',
            message:'删除成功！'
          })
        }).catch(()=>{
          this.$message({
            type:'info',
            message:'已取消'
          })
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/post/export', {
        ...this.queryParams
      }, `post_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

<style scoped>
/*设置图书详情框最小宽度 */
  ::v-deep .infoDialog{

  min-width: 600px;
  /* width: 800px; */
}
.el-table .fixed-width .el-button--mini {
  padding: 6px;
}

</style>
